package com.hongmeng.teamwork.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolExecutorConfig {

    /**
     * 配置线程池
     */
    @Bean("myThreadPoolExecutor")
    public ThreadPoolTaskExecutor myThreadPoolExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        /**
         * corePoolSize：线程池维护线程的最少数量
         * keepAliveSeconds：允许的空闲时间
         * maxPoolSize：线程池维护线程的最大数量
         * queueCapacity：缓存队列
         * rejectedExecutionHandler：拒绝策略
         */
        taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        taskExecutor.setMaxPoolSize(200);
        taskExecutor.setQueueCapacity(25);
        // 定义线程池名称
        taskExecutor.setThreadNamePrefix("hm-teamwork");
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();
        return taskExecutor;
    }
}
